#!/usr/bin/python
# -*- coding: utf-8 -*-

# project - description
# Copyright (C) 2010  Cédric Bonhomme - http://cedricbonhomme.org/
#
# For more information : http://bitbucket.org/cedricbonhomme/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.1 $"
__date__ = "$Date: 2011/02/25 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "GPLv3"

import cgi
from datetime import datetime

import utils
from pygeocoder import Geocoder


print "Content-Type: text/html\n\n"
print "<html>\n<head>\n"
print """<style type=text/css media=screen>
body{font:normal medium 'Gill Sans','Gill Sans MT',Verdana,sans-serif;margin:1.75em auto;width:40.8em;line-height:1.75}
blockquote{font-size:small;line-height:2.153846;margin:2.153846em 0;padding:0;font-style:oblique;border-left:1px dotted;margin-left:2.153846em;padding-left:2.153846em}
blockquote p{margin:2.153846em 0}
p+br{display:none}
h1{font-size:large}
h2,h3{font-size:medium}
#footer{clear:both;text-align:center;font-size:small}
img{border:0}
.horizontal,.simple li{margin:0;padding:0;list-style:none;display:inline}
.simple li:before{content:"+ "}
.simple > li:first-child:before{content:""}
.author{text-decoration:none;display:block;float:right;margin-left:2em;font-size:small}
ul{list-style:none;margin-left:0;padding-left:0}
li{clear:both}
.content{margin:1.75em 1.75em}
</style></head><body>\n"""
print  "<title>Where is Cedric?</title>\n</head>\n<body>\n"

locations = utils.get_locations()

if locations != []:
    print "<h1>Last location</h1>"
    google_map_url = "http://maps.google.com/maps/api/staticmap?center=" + \
                    locations[0][1] + "," + locations[0][0] + "&zoom=14&size=512x512&maptype=roadmap&sensor=false"
    print '<div align="center"><img src="' + google_map_url + '" align="center" /></div>'
    print 'Time: ' + datetime.fromtimestamp(float(locations[0][2])).isoformat()



    gcoder = Geocoder()
    print "<h1>History</h1>"
    print '<table border="1" width="100%">'
    for elem in locations[:10]:
        if elem[0] != '(none)':
            try:
                addresses = gcoder.reverse_geocode(float(elem[1]), float(elem[0]))
                country = addresses[0]['address_components'][4]['long_name']
                area = addresses[0]['address_components'][3]['long_name']
                city = addresses[0]['address_components'][1]['long_name']
            except Exception, e:
                city, country = "", ""

            google_map_url = "http://maps.google.com/maps/api/staticmap?center=" + elem[1] + \
                            "," + elem[0] + "&zoom=14&size=512x512&maptype=roadmap&sensor=false"
            print "<tr><td>" + city + ', ' + country + \
                            '<br />(<a href=' + google_map_url  +  '>map</a>)</td>'
            print "<td align='center'>" + str(elem[1]) + "</td><td align='center'>" + str(elem[0]) + \
                            "</td><td align='center'>" +  datetime.fromtimestamp(float(elem[2])).isoformat() + "</td></tr>"
    print "</table>"

else:
    print "No locations in the database."


print "<hr /><p align='center'>Data from Android.</p>\n"
print "</body>\n</html>\n"